package au.com.spaceandtime.coordinates;

import au.com.spaceandtime.math.Sign;

import java.util.logging.Logger;

/**
 * Degrees, minutes, seconds bean.
 * User: whale
 * Date: 19/10/13
 * Time: 2:42 PM
 */

public class DegMinSec {

    private static final Logger LOG = Logger.getLogger(DegMinSec.class.getName());

    int degrees = 0;
    int minutes = 0;
    float seconds = 0.0f;
    Sign sign = Sign.Positive;


    public DegMinSec(int degrees, int minutes, float seconds, Sign sign) {
        this.sign = sign;
        setDegrees(degrees);
        setMinutes(minutes);
        setSeconds(seconds);
    }

    public Sign getSign() {
        return sign;
    }

    public void setSign(Sign sign) {
        this.sign = sign;
    }

    public DegMinSec(double digitalDegrees) {

        LOG.info("digitalDegrees " + digitalDegrees);
        if (digitalDegrees < 0) {
            degrees = Math.abs((int)Math.ceil(digitalDegrees));
            sign = Sign.Negative;
        } else {
            degrees = (int)Math.floor(digitalDegrees);
            sign = Sign.Positive;
        }

        double remainder = Math.abs(digitalDegrees) - degrees;
        LOG.info("remainder1 " + remainder);

        if (digitalDegrees < 0) {
            minutes = (int)Math.abs(Math.floor(60*remainder));

            remainder = Math.abs((60*remainder)) - minutes;
            LOG.info("remainder2 " + remainder);
        }
        else {
            minutes = (int)Math.floor(60*remainder);
            remainder = Math.abs((60*remainder)) - minutes;
        }

        seconds = (float)(60*remainder);

    }

    public int getDegrees() {
        return degrees;
    }

    private void setDegrees(int degrees) {
        this.degrees = degrees;
    }

    public int getMinutes() {
        return minutes;
    }

    private void setMinutes(int minutes) {
        this.minutes = minutes;
    }

    public float getSeconds() {
        return seconds;
    }

    private void setSeconds(float seconds) {
        this.seconds = seconds;
    }

    public double degreesToDecimal() {

        double result = Math.abs(degrees) + ((double)minutes)/60d + ((double)seconds)/3600d;

        if (sign.isNegative()) {
            result *= -1;
        }

        return result;
    }
}
